import os
import re

def convert_content(content):
    # 转换标题和脚注
    content = re.sub(r'\\section\{(.*?)\}\\footnote\{(.*?)\}', r'== \1 #footnote[\2]', content)
    
    # 转换文言文环境
    content = re.sub(r'\\begin\{wenyan\}', r'#guwendoc(', content)
    content = re.sub(r'\\end\{wenyan\}', r')', content)
    
    # 移除列切换命令
    content = content.replace(r'\paracolswitchcolumn', '')
    
    # 确保段落格式正确
    content = re.sub(r'\n\s*\n', r'\n\n', content)
    return content

for filename in os.listdir('.'):
    if filename.endswith('.tex'):
        # 读取原文件内容
        with open(filename, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 转换内容
        new_content = convert_content(content)
        
        # 写入新文件
        new_filename = filename.replace('.tex', '.typ')
        with open(new_filename, 'w', encoding='utf-8') as f:
            f.write('#import "../styles.typ": *\n\n')
            f.write(new_content)
        
        print(f'转换完成: {filename} -> {new_filename}')